package org.ptkparty.client.activity;

import java.util.List;
import java.util.Set;

import javax.validation.ConstraintViolation;

import org.ptkparty.client.ClientFactory;
import org.ptkparty.client.Context;
import org.ptkparty.client.model.GameProxy;
import org.ptkparty.client.model.PlaceProxy;
import org.ptkparty.client.model.UserProxy;
import org.ptkparty.client.place.ResultsPlace;
import org.ptkparty.client.request.GameRequest;
import org.ptkparty.client.view.games.AddGameView;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.web.bindery.requestfactory.gwt.client.RequestFactoryEditorDriver;
import com.google.web.bindery.requestfactory.shared.Receiver;

public class AddGameActivity implements Activity {

	private static Driver driver = GWT.create(Driver.class);

	interface Driver extends RequestFactoryEditorDriver<GameProxy, AddGameView> {
	}

	private ClientFactory clientFactory;
	private GameRequest gameRequest;
	private AddGameView view;
	private String game;

	public AddGameActivity(ClientFactory clientFactory, String currentChampionship, String game) {
		this.clientFactory = clientFactory;
		this.game = game;
		Context.get().setCurrentChampionship(currentChampionship);
	}

	@Override
	public void start(AcceptsOneWidget panel, final EventBus eventBus) {
		view = clientFactory.getAddGameView();
		view.setPresenter(this);
		panel.setWidget(view);

		clientFactory.getFactory().userRequest().getUsersByChampionship(Context.get().getCurrentChampionship()).fire(new Receiver<List<UserProxy>>() {

			@Override
			public void onSuccess(List<UserProxy> response) {

				view.loadUser(response);

				clientFactory.getFactory().placeRequest().getAll().fire(new Receiver<List<PlaceProxy>>() {

					@Override
					public void onSuccess(List<PlaceProxy> places) {
						view.loadPlaces(places);

						driver.initialize(eventBus, clientFactory.getFactory(), view);

						if (game != null) {
							clientFactory.getFactory().gameRequest().findGameEntity(game).fire(new Receiver<GameProxy>() {

								@Override
								public void onSuccess(GameProxy response) {
									gameRequest = clientFactory.getFactory().gameRequest();
									initEdit(response);
								}
							});

						} else {
							gameRequest = clientFactory.getFactory().gameRequest();
							GameProxy gameProxy = gameRequest.create(GameProxy.class);
							gameProxy.setChampionship(Context.get().getCurrentChampionship());

							initEdit(gameProxy);
						}
					}
				});
			}

		});

	}

	private void initEdit(GameProxy gameProxy) {
		driver.edit(gameProxy, gameRequest);

		gameRequest.createGame().using(gameProxy).to(new Receiver<Void>() {

			@Override
			public void onConstraintViolation(Set<ConstraintViolation<?>> violations) {
				driver.setConstraintViolations(violations);
			}

			@Override
			public void onSuccess(Void response) {
				clientFactory.getPlaceController().goTo(new ResultsPlace(Context.get().getCurrentChampionship()));
			}
		});
	}

	@Override
	public String mayStop() {
		return null;
	}

	@Override
	public void onCancel() {
	}

	@Override
	public void onStop() {
	}

	public void save() {
		driver.flush().fire();
	}

}
